var express = require('express');
var router = express.Router();
var Manager = require('../modules/mongo');
var async = require('async');
var DBManager =new Manager();

router.post('/addStudents',function(req, res, next){
    let req_data = req.body
    let bj_data = {
        bjName: req_data.bjName,
        gradeName: req_data.gradeName
    }
    let params ={
        studentID: req_data.studentID,
        studentName: req_data.studentName,
        age: req_data.age,
        phone: req_data.phone,
        address: req_data.address,
        create_time: new Date(),
        update_time: new Date()
    }
    async.waterfall([
        function (cb) {
            DBManager.find('bj',bj_data, {}, result => {
                if(result.success){
                    let n =result.result[0]._id+''
                    cb(null, n)
                }
            })
        },
        function (n, cb){
            params.bjid = n
            DBManager.insert('students', params, result =>{
                if(result.success){
                    res.send({success:true, code:0,message:'新增用户成功'})
                }else{
                    res.send({success:false,code:1,message:'上传失败'});
                }
            })
        }], 
        function(err,result){
            console.log('***')
    })
});

router.get('/',function(req, res, next){
    let req_data = req.query
    let bj_data = {
        bjName: {$regex: req_data.bjName, $options: 'i' },
        gradeName: {$regex: req_data.gradeName, $options: 'i' }
    }
    let studentsquery = {
        studentID: {$regex: req_data.studentID, $options: 'i' },
        studentName: {$regex: req_data.studentName, $options: 'i' },
        phone: {$regex: req_data.phone, $options: 'i' }
    }
    async.parallel({
        getbj: function(cb) { 
           DBManager.find('bj', bj_data, {}, result => {
               if(result.success){
                   cb(null, result.result)
               }
           })
        },
        getstudent: function(cb) {
            DBManager.find('students', studentsquery, {}, result => {
                if(result.success){
                    cb(null, result.result)
                }
            })
        },
    }, function (err, results) {
      let studentInfor = []
      results.getstudent.map(function(item){
        let bjInfor = results.getbj.find(function(currentValue){
            return currentValue._id+'' === item.bjid
        })
        let obj = Object.assign({},item,bjInfor)
        studentInfor.push(obj)
      })
      if(!err){
          res.send({success:true, result: studentInfor, message:'查询完成'})
          return 
      }
      res.send({success:false, result: [], message:'查询失败'})
    })
})

router.post('/deleteStudents',function(req, res, next){
    let req_data = req.body
    DBManager.delete('students', {_id: req_data._id}).then(respone =>{
        res.send(respone)
    }).catch(err => {
        console.log(err)
    })
})

router.post('/updataStudents',function(req, res, next){
    let req_data = req.body
    let bj_data = {
        bjName: req_data.bjName,
        gradeName: req_data.gradeName
    }
    let params ={
        studentID: req_data.studentID,
        studentName: req_data.studentName,
        age: req_data.age,
        phone: req_data.phone,
        address: req_data.address,
        update_time: new Date()
    }
    let whereData = {
        _id: req_data._id
    }
    async.waterfall([
        function (cb) {
            DBManager.find('bj',bj_data, {}, result => {
                if(result.success){
                    let n =result.result[0]._id+''
                    cb(null, n)
                }
            })
        },
        function (n, cb){
            params.bjid = n
            DBManager.update('students', whereData, {$set: params})
            .then(result => {
                if(result.success){
                  res.send({success:true, code:0,message:'更新用户成功'})
                }
            }).catch(err => {
                res.send({success:false, code:1,message:'更新用户失败'})
            })
        }], 
        function(err,result){
            console.log('***')
    })
});

module.exports = router;